import { schemaInstances } from '@neu/cmdb-ui/api/instance'

const store = {
  promiseList: [],
  fetch(schema) {
    const that = this
    const cachePromise = that.checkPromise(schema)
    if (cachePromise) {
      return cachePromise
    }

    const promise = schemaInstances(schema)

    that.promiseList.push({
      key: schema,
      value: promise
    })
    return promise
  },
  checkPromise(schema) {
    const key = schema
    const res = this.promiseList.filter(item => item.key === key)
    if (res.length > 0) {
      return res[0].value
    } else {
      return false
    }
  }
}

export default store
